package org.coody.czone.web.blog.service;

import java.util.List;

import javax.annotation.Resource;

import org.coody.czone.common.constant.CacheFinal;
import org.coody.czone.common.jdbc.JdbcTemplate;
import org.coody.czone.web.blog.domain.JournalInfo;
import org.coody.framework.cache.annotation.CacheWrite;
import org.coody.framework.core.annotation.InitBean;
import org.coody.framework.core.util.StringUtil;
import org.coody.framework.jdbc.entity.Pager;
import org.coody.framework.jdbc.entity.Where;

@InitBean
public class JournalService {

	@Resource
	JdbcTemplate jdbcTemplate;
	
	@CacheWrite(key=CacheFinal.JOURNAL_PAGER)
	public Pager loadPager(String userId,JournalInfo journal,Pager pager){
		Where where=new Where();
		where.set("userId", userId);
		return jdbcTemplate.findPager(journal,where,pager,"id",false);
	}
	/**
	 * 加载最新文章
	 */
	@CacheWrite(key=CacheFinal.JOURNAL_LIST_NEWSD,time=600)
	public List<JournalInfo> loadNews(String userId){
		Pager pager=new Pager();
		pager.setPageSize(6);
		Where where=new Where();
		where.set("userId", userId);
		pager= jdbcTemplate.findPager(JournalInfo.class,where,pager,"id",true);
		if(StringUtil.isNullOrEmpty(pager.getData())){
			return null;
		}
		return pager.getData();
	}
	
	/**
	 * 加载最新文章
	 */
	@CacheWrite(key=CacheFinal.JOURNAL_LIST_HOTSD,time=600)
	public List<JournalInfo> loadHots(String userId){
		Pager pager=new Pager();
		pager.setPageSize(6);
		Where where=new Where();
		where.set("userId", userId);
		pager= jdbcTemplate.findPager(JournalInfo.class,where,pager,"views",true);
		if(StringUtil.isNullOrEmpty(pager.getData())){
			return null;
		}
		return pager.getData();
	}
	
}
